package com.every.love.girl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.every.love.girl.model.PicModel;
import com.lizhi.updata.UploadDataTask;
import com.lizhi.util.Util;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;

public class LoveService {
	String host = "https://lovegirl.firebaseio.com/pics.json";
	List<String> urlList = new ArrayList<String>();

	public LoveService() {
		Util.clearData(host);
		try {
			File file = new File("/Users/lixinke/Documents/workspace/test/data.txt");
			FileInputStream inputStream = new FileInputStream(file);
			List<String> urls = readStream(inputStream);

			Calendar calendar = Calendar.getInstance();
			calendar.set(2015, 2, 15, 10, 16);

			int i = 0;
			for (String string : urls) {
				PicModel picModel = new PicModel();
				picModel.url = string;
				Image img = Image.getInstance(new URL(string));
				picModel.width= String.valueOf(img.width());
				picModel.height=String.valueOf(img.height());
				picModel.price = "5";
				picModel.time = String.valueOf(calendar.getTimeInMillis());
				calendar.add(Calendar.MILLISECOND, 1);
				if (i % 10 == 0) {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
				System.out.println(JSON.toJSONString(picModel));
				new UploadDataTask(host, JSON.toJSONString(picModel)).start();
				i++;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<String> readStream(InputStream inputStream) {
		List<String> urls = new ArrayList<String>();
		StringBuffer strContent = new StringBuffer();
		String strLine;
		try {
			InputStreamReader streamReader = new InputStreamReader(inputStream);
			BufferedReader reader = new BufferedReader(streamReader);
			while ((strLine = reader.readLine()) != null) {
				strContent.append(strLine);
				strContent.append("\n");
				urls.add(strLine);
			}
			streamReader.close();
			reader.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return urls;
	}

	public static void main(String[] args) {
		new LoveService();
	}

}
